package com.ruoyi.product.config;

import com.ruoyi.product.pojo.ResponEntity;
import com.ruoyi.product.util.ExceptionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@RestControllerAdvice//AOP
@Slf4j
//AOP 前置 后置 环绕  返回  异常
public class UserExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponEntity error(Exception e){
        e.printStackTrace();
        System.out.println("执行了全局异常处理类");
//        log.error(ExceptionUtils.getMessage(e));
        log.info(e.getMessage());
        log.error(e.getMessage());
        return new ResponEntity(null,e.getMessage(),5000,false);
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResponEntity runtime(Exception e){
        e.printStackTrace();
        System.out.println("调用了RuntimeException公共异常处理类");
//        log.error(ExceptionUtils.getMessage(e));
        log.info(e.getMessage());
        log.error(e.getMessage());
        return new ResponEntity(null,e.getClass().getName()+"==>" + e.getClass(),5000,false);
    }

    @ExceptionHandler(SQLException.class)
    @ResponseBody
    public ResponEntity sql(Exception e){
        e.printStackTrace();
        System.out.println("调用了SQLException公共异常处理类");
//        log.error(ExceptionUtils.getMessage(e));
        log.info(e.getMessage());
        log.error(e.getMessage());
        return new ResponEntity(null,e.getMessage(),5000,false);
    }

}
